package com.cmic.rcs.emoji.controller.app;

import com.cmic.rcs.emoji.entity.pojo.TApp;
import com.cmic.rcs.emoji.mapper.TAppMapper;
import com.cmic.rcs.enhance.message.g5.core.common.Response;
import com.cmic.rcs.enhance.message.g5.core.common.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/app/version")
@Slf4j
public class VersionController {

    private TAppMapper appMapper;

    public VersionController(TAppMapper appMapper) {
        this.appMapper = appMapper;
    }

    /**
     * 获取应用版本信息
     *
     * @return 包含应用logo、应用名称和版本号的响应
     */
    @PostMapping("/getAppInfo")
    public Response getAppInfo() {
        try {
            log.info("获取应用版本信息请求");

            // 从数据库获取最新的应用信息
            TApp latestAppInfo = appMapper.getLatestAppInfo();

            // 构建响应数据
            Map<String, String> appInfo = new HashMap<>();
            if (latestAppInfo != null) {
                appInfo.put("appLogo", latestAppInfo.getAppLogoUrl());
                appInfo.put("appName", latestAppInfo.getAppName());
                appInfo.put("version", latestAppInfo.getAppVersion());
                log.info("成功获取应用版本信息: {} - {}", latestAppInfo.getAppName(), latestAppInfo.getAppVersion());
            } else {
                // 如果数据库中没有应用信息，使用默认值
                appInfo.put("appLogo", "");
                appInfo.put("appName", "新基建视窗");
                appInfo.put("version", "V1.0");
                log.warn("数据库中未找到应用信息，使用默认值");
            }

            return ResponseData.OK(appInfo);
        } catch (Exception e) {
            log.error("获取应用版本信息异常: {}", e.getMessage(), e);

            // 异常情况下返回默认值
            Map<String, String> defaultAppInfo = new HashMap<>();
            defaultAppInfo.put("appLogo", "");
            defaultAppInfo.put("appName", "新基建视窗");
            defaultAppInfo.put("version", "V1.0");

            return ResponseData.OK(defaultAppInfo);
        }
    }
}
